/*****************************************************************
Copyright (c) 2011 - 2015 Qualcomm Technologies International, Ltd.

PROJECT
    source
    
FILE NAME
    source_led_handler.c

DESCRIPTION
    Updates LED indications.
    The functionality is only included if INCLUDE_LEDS is defined.
*/


#ifdef INCLUDE_LEDS

/* header for this file */
#include "source_led_handler.h"
/* application header files */
#include "source_debug.h"
#include "source_private.h"
#include "source_states.h"
#include "source_a2dp.h"
#include "source_memory.h"
#include "source_power.h"
#include "source_buttons.h"
#include <usb.h>
#include <logging.h>
/* profile/library headers */

/* VM headers */

#ifdef DEBUG_LEDS
    #define LEDS_DEBUG(x) SOURCE_DEBUG(x)
#else
    #define LEDS_DEBUG(x)
#endif

/***************************************************************************
Functions
****************************************************************************
*/

/****************************************************************************
NAME    
    leds_show_state - Updates the LED pattern based on the current device state  
*/
void leds_show_state(bool primaryDevice, SOURCE_STATE_T state)
{  
    LEDS_DEBUG(("LED : Primary=%d State %d\n", primaryDevice, state));
    
    switch(state)
    {
        case SOURCE_STATE_INITIALISING:
        {
            ledsPlay(LED_GROUP_A, LED_OFF_STATE);
            ledsPlay(LED_GROUP_B, LED_OFF_STATE);
            ledsPlay(LED_GROUP_C, LED_OFF_STATE);
        }
        break;

        case SOURCE_STATE_POWERED_OFF:
        {
            ledsPlay(LED_GROUP_C, LED_NO_CHARGING);
            ledsPlay(LED_GROUP_A, LED_OFF_STATE);
            ledsPlay(LED_GROUP_B, LED_OFF_STATE);
            ledsPlay(LED_GROUP_A, LED_POWER_OFF_STATE);
        }
        break;

        case SOURCE_STATE_CONNECTABLE:
        {
            if(primaryDevice)
            {
                ledsPlay(LED_GROUP_A, LED_GROUP_A_CONNECTABLE_STATE);
            }
            else
            {
                ledsPlay(LED_GROUP_B, LED_GROUP_B_CONNECTABLE_STATE);
            }
        }
        break;

        case SOURCE_STATE_INQUIRING:
        {
            if(primaryDevice)
            {
                ledsPlay(LED_GROUP_A, LED_GROUP_A_INQUIRING_STATE);
            }
            else
            {
                ledsPlay(LED_GROUP_B, LED_GROUP_B_INQUIRING_STATE);
            }
        }
        break;

        case SOURCE_STATE_CONNECTING:
        {
            if(primaryDevice)
            {
                ledsPlay(LED_GROUP_A, LED_GROUP_A_CONNECTING_STATE);
            }
            else
            {
                ledsPlay(LED_GROUP_B, LED_GROUP_B_CONNECTING_STATE);
            }
        }
        break;
        
        default:
        {
            /* do nothing */
        }
        break;
    }
}


/****************************************************************************
NAME    
    leds_show_event - Updates the LED pattern based on the current device event  
*/
void leds_show_event(LED_EVENT_T event)
{
    LEDS_DEBUG(("LED : Event %d\n", event));

    switch(event)
    {
    case LED_EVENT_POWER_ON:
    {
        ledsPlay(LED_GROUP_A, LED_POWER_ON);
    }
    break;

    case LED_EVENT_BATT_LVL_0:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_0);
    }
    break;

    case LED_EVENT_BATT_LVL_1:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_1);
    }
    break;

    case LED_EVENT_BATT_LVL_2:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_2);
    }
    break;

    case LED_EVENT_BATT_LVL_3:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_3);
    }
    break;

    case LED_EVENT_BATT_LVL_0_0:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_0_0);
    }
    break;

    case LED_EVENT_BATT_LVL_1_1:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_1_1);
    }
    break;

    case LED_EVENT_BATT_LVL_2_2:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_2_2);
    }
    break;

    case LED_EVENT_BATT_LVL_3_3:
    {
        ledsPlay(LED_GROUP_C, LED_BATT_LEVEL_3_3);
    }
    break;

    case LED_EVENT_CHARGING_LVL_0:
    {
        ledsPlay(LED_GROUP_C, LED_CHARGING_BATT_LVL_0);
    }
    break;

    case LED_EVENT_CHARGING_LVL_1:
    {
        ledsPlay(LED_GROUP_C, LED_CHARGING_BATT_LVL_1);
    }
    break;

    case LED_EVENT_CHARGING_LVL_2:
    {
        ledsPlay(LED_GROUP_C, LED_CHARGING_BATT_LVL_2);
    }
    break;

    case LED_EVENT_CHARGING_LVL_3:
    {
        ledsPlay(LED_GROUP_C, LED_CHARGING_BATT_LVL_3);
    }
    break;

    case LED_EVENT_CHARGING_FULL:
    {
        ledsPlay(LED_GROUP_C, LED_CHARGING_BATT_FULL);
    }
    break;

    case LED_EVENT_CHARGER_DETECH:
    {
        ledsPlay(LED_GROUP_C, LED_NO_CHARGING);
    }
    break;

    case LED_EVENT_FACTORY_RESET:
    {
        ledsPlay(LED_GROUP_C, LED_NO_CHARGING);
        ledsPlay(LED_GROUP_A, LED_FACTORY_RESET);
    }
    break;

    default:
    {

    }
    break;
    }
}

void led_show_codec(bool primaryDevice, uint8_t seid)
{
    LEDS_DEBUG(("LED : SEID %d\n", seid));

    switch(seid)
    {
    case A2DP_SEID_SBC:
        if(primaryDevice)
        {
            ledsPlay(LED_GROUP_A, LED_GROUP_A_APTX_LL_CODEC);
        }
        else
        {
            ledsPlay(LED_GROUP_B, LED_GROUP_B_APTX_LL_CODEC);
        }

        break;
    case A2DP_SEID_APTX_LOW_LATENCY:
        if(primaryDevice)
        {
            ledsPlay(LED_GROUP_A, LED_GROUP_A_APTX_LL_CODEC);
        }
        else
        {
            ledsPlay(LED_GROUP_B, LED_GROUP_B_APTX_LL_CODEC);
        }
        break;
    case A2DP_SEID_APTX:
        if(primaryDevice)
        {
            ledsPlay(LED_GROUP_A, LED_GROUP_A_APTX_LL_CODEC);
        }
        else
        {
            ledsPlay(LED_GROUP_B, LED_GROUP_B_APTX_LL_CODEC);
        }
        break;
    case A2DP_SEID_APTXHD:
        if(primaryDevice)
        {
            ledsPlay(LED_GROUP_A, LED_GROUP_A_APTX_LL_CODEC);
        }
        else
        {
            ledsPlay(LED_GROUP_B, LED_GROUP_B_APTX_LL_CODEC);
        }
        break;
    case A2DP_SEID_APTXAD:
        if(primaryDevice)
        {
            ledsPlay(LED_GROUP_A, LED_GROUP_A_APTX_LL_CODEC);
        }
        else
        {
            ledsPlay(LED_GROUP_B, LED_GROUP_B_APTX_LL_CODEC);
        }
        break;
    default:
        break;
    }

    ledSync(LED_GROUP_A, LED_GROUP_B);
}

#endif /* INCLUDE_LEDS */

